package com.matchmaking.system.controller;


import cn.hutool.core.lang.Snowflake;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.matchmaking.system.config.R;
import com.matchmaking.system.domain.District;
import com.matchmaking.system.domain.SysUser;
import com.matchmaking.system.service.DistrictService;
import com.matchmaking.system.service.SysUserService;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("district")
@CrossOrigin
public class DistrictController {
    @Autowired
    DistrictService districtService;



    /**
     * 导出
     * @return
     */
    @PostMapping("export")
    private void export(@RequestBody District district, HttpServletResponse response) throws IOException {
        List<District> districts = districtService.DistrictExport(district);
        districtService.exprot(districts,response);
    }


    /**
     * 状态
     * @return
     */
    @PostMapping("statusDistrict")
    public R statusDistrict(@RequestBody District district){
        districtService.updateStatus(district);
        if ("0".equals(district.getStatus())){
            return R.successed("状态正常");
        }else if ("1".equals(district.getStatus())){
            return R.successed("状态停用");
        }
        return R.successed(district.getStatus());
    }

    /**
     * 查询
     * @return
     */
    @PostMapping("lists")
    public List<District> lists(@RequestBody District district){

        List<District> lists = districtService.selectObjects(district);
        return lists;
    }

    /**
     * 修改
     * @return
     */
    @PostMapping("updates")
    public boolean updates(@RequestBody District district){
        Integer integer = districtService.updateObjects(district);

        if (integer>0){
            return true;
        }else {
            return false;
        }

    }
    /**
     * 雪花算法
     * @return
     */
    @GetMapping("xuehua")
    public String xuehua(){
        String s = new Snowflake().nextIdStr();
        return s;
    }

    /**
     * 添加
     * @return
     */
    @PostMapping("adds")
    public boolean adds(@RequestBody District district){
//
//        int insertObjects = districtService.insertObjects(district);
//
//        if(insertObjects>0){
//            return true;
//        }else {
//            return false;
//        }
        if (district.getLevel()!=null){
            District dis = districtService.selectCityNameLevel(district);

            district.setLevel(district.getLevel()+1);
            district.setParentId(dis.getId());
        }else {
            district.setParentId(0);
            district.setLevel(0);
        }

        int i = districtService.insertObject(district);
        if (i>=0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 根据id删除
     * @return
     */
    @GetMapping("deletes")
    public boolean deletes(Integer id){

        int b = districtService.removeObjects(id);

        if (b>=0){
            return true;
        }else {
            return false;
        }

    }

    /**
     * 城市等级统计
     * @return
     */
    @GetMapping("excharDistrict")
    public Map<String, Object> excharDistrict(){
        HashMap<String, Object> map = new HashMap<>();
        List<District> districtList = districtService.findexcharDistrict();
        ArrayList<Integer> arrayList = new ArrayList<>();
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        for (District district : districtList) {
            arrayList.add(district.getLevel());
            arrayList1.add(district.getNum());
        }
        map.put("x",arrayList);
        map.put("y",arrayList1);
        map.put("code",200);

        return map;
    }

    /**
     * 首字母统计
     *
     * @return
     */
    @GetMapping("echarFirstLetter")
    public List<District> echarFirstLetter(){
        List<District> districtList = districtService.findFirstLetter();
        return districtList;
    }

    /**
     * 城市等级统计折线图
     * @return
     */
    @GetMapping("levelDistrict")
    public Map<String,Object> levelDistrict(){
        HashMap<String, Object> map = new HashMap<>();
        List<District> districtList = districtService.leveldists();
        ArrayList<Integer> arrayList = new ArrayList<>();
        ArrayList<Integer> arrayList1 = new ArrayList<>();
        for (District district : districtList) {
            arrayList.add(district.getLevel());
            arrayList1.add(district.getNum());
        }
        map.put("x1",arrayList);
        map.put("y1",arrayList1);
        map.put("code",200);
        return map;
    }



}
